package com.hanrui.android.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hanrui.android.R;
import com.hanrui.android.adapter.AddressListAdapter;
import com.hanrui.android.constant.LoadType;
import com.hanrui.android.library.base.ActionBarBaseActivity;
import com.hanrui.android.model.AddressListBean;
import com.hanrui.android.net.contract.addresslist.AddressContract;
import com.hanrui.android.net.presenter.addresslist.AddressPresenter;
import com.hanrui.android.utils.DialogHelper;
import com.hanrui.android.widget.SearchEditText;
import com.hanrui.android.widget.dialog.ActionSheetDialog;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 人员通讯录
 */
public class AddressListActivity extends ActionBarBaseActivity<AddressPresenter> implements AddressContract.View, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @Inject
    AddressListAdapter adapter;
    @BindView(R.id.searchEditText)
    SearchEditText searchEditText;
    String id;

    String name = "";

    @Override
    protected void initInjector() {mActivityComponent.inject(this);}


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_mobile);

        unbinder = ButterKnife.bind(this);

        id = getIntent().getStringExtra("id");

        initView();

    }

    private void initView() {

        mTitleBar.setTitle("通讯录");

        setIsPullRefresh(true);


        setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                name = "";
                mPresenter.companyList(AddressListActivity.this,id,name);


            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(adapter);
        adapter.setEmptyView(getNoDataView(mRecyclerView));
        adapter.setEnableLoadMore(false);
        adapter.setOnItemClickListener(this);


        searchEditText.setOnSearchClickListener(new SearchEditText.OnSearchClickListener() {
            @Override
            public void onSearchClick(View view, String keyword) {

                name = searchEditText.getText().toString();
                mPresenter.companyList(AddressListActivity.this,id,name);

            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        setRefreshing();
        mPresenter.companyList(this,id,name);
    }

    @Override
    public void companyList(List<AddressListBean.NoAttendListBean> dataList) {

        setLoadDataResult(adapter,dataList, LoadType.TYPE_REFRESH_SUCCESS,false);


    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        final AddressListBean.NoAttendListBean bean = (AddressListBean.NoAttendListBean) adapter.getItem(position);
        new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem(bean.getName(), ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                    }
                }).addSheetItem(bean.getUserTypeName(), ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() {
            @Override
            public void onClick(int which) {
            }
        }).addSheetItem(bean.getMobilePhone(), ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() {
            @Override
            public void onClick(int which) {

                if (TextUtils.isEmpty(bean.getMobilePhone())) {
                    return;
                }
                callPhone(bean.getMobilePhone());

            }
        }).show();

    }

    private void callPhone(final String mobilePhone) {

        PermissionUtils.permission(PermissionConstants.PHONE)
                .rationale(new PermissionUtils.OnRationaleListener() {
                    @Override
                    public void rationale(final ShouldRequest shouldRequest) {
                        DialogHelper.showRationaleDialog(shouldRequest);
                    }
                })
                .callback(new PermissionUtils.FullCallback() {
                    @Override
                    public void onGranted(List<String> permissionsGranted) {
                        LogUtils.d(permissionsGranted);

                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+mobilePhone));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);

                    }

                    @Override
                    public void onDenied(List<String> permissionsDeniedForever,
                                         List<String> permissionsDenied) {
                        if (!permissionsDeniedForever.isEmpty()) {
                            DialogHelper.showOpenAppSettingDialog();
                        }
                        LogUtils.d(permissionsDeniedForever, permissionsDenied);
                    }
                })
                .request();
    }



    public static void start(Context context,String id){

        Intent intent = new Intent(context,AddressListActivity.class);
        intent.putExtra("id",id);
        context.startActivity(intent);

    }

}
